/**
 * Program   :   SecurityServiceImpl.java
 * Author    :   shadow guo
 * Create    :   2013-6-11 下午10:44:35
 *
 * Copyright 2008 by Tongfu Information Technology Co. Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Tongfu Information Technology Co. Ltd. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Tongfu Information Technology Co. Ltd.
 * 
 */
package com.tongfusoft.example.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

import com.tongfusoft.da.help.DBUtils;
import com.tongfusoft.example.data.TPriviledge;
import com.tongfusoft.example.data.TResources;
import com.tongfusoft.utils.CryptoUtils;
import com.tongfusoft.utils.MapUtils;
import com.tongfusoft.web.core.Constant;
import com.tongfusoft.web.core.data.PrivilegeData;
import com.tongfusoft.web.core.data.ResourceData;
import com.tongfusoft.web.core.data.User;
import com.tongfusoft.web.core.service.ISecurityService;

public class SecurityServiceImpl implements ISecurityService{

	@Override
	public Collection<GrantedAuthority> loadUserAuthoritiesByName(
			String userName) {
		List<Map<String,String>> list = DBUtils.executeSelect("/security#priviledgeByUserName",  MapUtils.factory().put("userName", userName).create());
		if(list != null && list.size() > 0){
			Collection<GrantedAuthority> result = new ArrayList<GrantedAuthority>(list.size());
			for(int i=0;i<list.size();i++){
				result.add(new GrantedAuthorityImpl(list.get(i).get("PRI_NAME")));
			}
			
			return result;
		}
		
		return Collections.EMPTY_LIST;
	}

	@Override
	public String getPwdByName(String userName) {
		List<Map<String,String>> list = DBUtils.executeSelect("/security#userPwdByName", MapUtils.factory().put("userName", userName).create());
		if(list != null && list.size() > 0){
			return list.get(0).get("USER_PWD");
		}
		
		return null;
	}

	@Override
	public List<PrivilegeData> getAllPrivilege() {
		List<TPriviledge> list = DBUtils.executeSelect("/security#AllPriviledge", TPriviledge.class, Integer.MAX_VALUE, 0, new HashMap());
		if(list != null && list.size() > 0){
			List<PrivilegeData> result = new ArrayList<PrivilegeData>();
			for(TPriviledge priviledge : list){
				result.add((PrivilegeData)priviledge);
			}
			
			return result;
		}else{
			return Collections.EMPTY_LIST;
		}
	}

	@Override
	public List<ResourceData> getResroucesByPrivilege(PrivilegeData privilege) {
		List<TResources> list = DBUtils.executeSelect("/security#resourcesByPriviledge", TResources.class, Integer.MAX_VALUE, 0, MapUtils.factory().put("priId", privilege.getPkValue()).create());
		if(list != null && list.size() > 0){
			List<ResourceData> result = new ArrayList<ResourceData>();
			for(TResources resources : list){
				result.add((ResourceData)resources);
			}
			
			return result;
		}else{
			return Collections.EMPTY_LIST;
		}
	}

	@Override
	public User getUserInfo(String userName) {
		List<User> list = DBUtils.executeSelect("/security#userInfoByName", MapUtils.factory().put("userName", userName).create());
		if(list != null && list.size() == 1){
			return list.get(0);
		}
		
		return null;
	}

	@Override
	public boolean validateUser(Map params) {
		if(StringUtils.equals(CryptoUtils.getMD5((String)params.get("j_password")), this.getPwdByName((String)params.get("j_username")))){
			return true;
		}
		
		return false;
	}

	@Override
	public Map<String, Object> getPredefineContent(User user, Map params) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put(Constant.CURRPROJECT, params.get(Constant.DEFAULTPROJECT));
		
		return map;
	}

}
